﻿using System;
using System.Drawing.Imaging;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Animation;

namespace CustomerAssistant
{
    /// <summary>
    /// InfoTip.xaml 的交互逻辑
    /// </summary>
    public partial class InfoTip : UserControl
    {
        bool IsMouseDown = false;
        Point mousePoint;
        public bool FadeEffect = false;

        public bool ShowMinimizedBtn { get; set; }

        public InfoTip()
        {
            InitializeComponent();
            myInitializeComponent();
        }

        private void myInitializeComponent()
        {
            this.Width = this.canvas1.Width;
            this.image2.Width = this.image2.Source.Width;
            this.image2.Height = this.image2.Source.Height;
            this.image3.Width = this.image3.Source.Width;
            this.image3.Height = this.image3.Source.Height;
            this.image4.Width = this.image4.Source.Width;
            this.image4.Height = this.image4.Source.Height;
            this.image2.Margin = new Thickness()
            {
                Top = 0
                ,
                Right = image3.Width
                ,
                Left = this.Width - this.image2.Width - this.image3.Width
                ,
                Bottom = this.Height - this.image2.Height
            };
            this.image3.Margin = new Thickness()
            {
                Top = 0
                ,
                Right = 0
                ,
                Left = this.Width - this.image3.Width
                ,
                Bottom = this.Height - this.image3.Height
            };
            this.image2.MouseMove += new MouseEventHandler((obj, arg) =>
            {
                this.image2.Source = SysHelper.GetImageSource(Properties.Resource.ninimize_2_hover, ImageFormat.Png);
                this.image2.Width = this.image2.Source.Width;
                this.image2.Height = this.image2.Source.Height;
                this.image2.Margin = new Thickness()
                {
                    Top = 0
                    ,
                    Right = image3.Width
                    ,
                    Left = this.Width - this.image2.Width - this.image3.Width
                    ,
                    Bottom = this.Height - this.image2.Height
                };
            });
            this.image2.MouseLeave += new MouseEventHandler((obj, arg) =>
            {
                this.image2.Source = SysHelper.GetImageSource(Properties.Resource.ninimize_2, ImageFormat.Png);
                this.image2.Width = this.image2.Source.Width;
                this.image2.Height = this.image2.Source.Height;
                this.image2.Margin = new Thickness()
                {
                    Top = 0
                    ,
                    Right = image3.Width
                    ,
                    Left = this.Width - this.image2.Width - this.image3.Width
                    ,
                    Bottom = this.Height - this.image2.Height
                };
            });

            this.image3.MouseMove += new MouseEventHandler((obj, arg) =>
            {
                this.image3.Source = SysHelper.GetImageSource(Properties.Resource.delete_2_hover, ImageFormat.Png);
                this.image3.Width = this.image3.Source.Width;
                this.image3.Height = this.image3.Source.Height;
                this.image3.Margin = new Thickness()
                {
                    Top = 0
                    ,
                    Right = 0
                    ,
                    Left = this.Width - this.image3.Width
                    ,
                    Bottom = this.Height - this.image3.Height
                };
            });
            this.image3.MouseLeave += new MouseEventHandler((obj, arg) =>
            {
                this.image3.Source = SysHelper.GetImageSource(Properties.Resource.delete_2, ImageFormat.Png);
                this.image3.Width = this.image3.Source.Width;
                this.image3.Height = this.image3.Source.Height;
                this.image3.Margin = new Thickness()
                {
                    Top = 0
                    ,
                    Right = 0
                    ,
                    Left = this.Width - this.image3.Width
                    ,
                    Bottom = this.Height - this.image3.Height
                };
            });
        }

        private void UserControl_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                IsMouseDown = true;
                mousePoint = e.GetPosition(this);
            }
        }

        private void UserControl_MouseMove(object sender, MouseEventArgs e)
        {
            if (IsMouseDown)
            {
                if (e.LeftButton == MouseButtonState.Pressed)
                {
                    var popup = Parent as Window;
                    Point mousePoint2 = e.GetPosition(this);
                    var h = popup.Left - (mousePoint.X - mousePoint2.X);

                    var v = popup.Top - (mousePoint.Y - mousePoint2.Y);

                    if (h <= (SystemParameters.PrimaryScreenWidth - this.Width) && h >= 0)
                        popup.Left = h;
                    else
                        mousePoint.X = mousePoint2.X;
                    if (v <= (SystemParameters.PrimaryScreenHeight - this.Height) && v >= 0)
                        popup.Top = v;
                    else
                        mousePoint.Y = mousePoint2.Y;
                }
            }
        }

        private void UserControl_MouseUp(object sender, MouseButtonEventArgs e)
        {
            if (IsMouseDown)
            {
                IsMouseDown = false;
            }
        }

        private void image3_MouseDown(object sender, MouseButtonEventArgs e)
        {
            Application.Current.Shutdown();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
        }

        private void image2_MouseDown(object sender, MouseButtonEventArgs e)
        {
            (this.Parent as Window).WindowState = WindowState.Minimized;
        }

        private void image3_MouseDown_1(object sender, MouseButtonEventArgs e)
        {
            var wnd = (this.Parent as Window);
            if (FadeEffect)
            {
                DoubleAnimation animation = new DoubleAnimation(1.00, 0.01, new Duration(TimeSpan.FromMilliseconds(200)));
                animation.Completed += new EventHandler((a, b) =>
                {
                    wnd.Close();
                });
                wnd.BeginAnimation(Window.OpacityProperty, animation);
            }
            else
            {
                wnd.Close();
            }
        }

        protected override void OnRender(System.Windows.Media.DrawingContext drawingContext)
        {
            var parentWnd = (this.Parent as Window);
            if (parentWnd != null && !string.IsNullOrEmpty(parentWnd.Title))
                txtTitle.Text = parentWnd.Title;
            this.image2.Visibility = ShowMinimizedBtn ? System.Windows.Visibility.Visible : System.Windows.Visibility.Hidden;
        }
    }
}